//
//  SecondViewController.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/15.
//

import UIKit

class LYUserViewController: LYBaseViewController {
    
    let userInfoView = LYUserInfoView()
    
    let actionView = LYUserActionView()
    
    private lazy var privacyAggrementImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        if appCountryEnv == .indonesia {
            iv.image = UIImage(named: "user_privacy_agreement_id")
        } else {
            iv.image = UIImage(named: "user_privacy_agreement")
        }

        return iv
    }()
    
    private lazy var privacyAggrementButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(privacyButtonAction(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var serviceImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        if appCountryEnv == .indonesia {
            iv.image = UIImage(named: "user_service_online_id")
        } else {
            iv.image = UIImage(named: "user_service_online")
        }
        return iv
    }()
    
    private lazy var serviceButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(serviceButtonAction(_:)), for: .touchUpInside)
        return button
    }()
    
    private let scrollView = UIScrollView()
    private let contentView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.isHiddenCustomTabBar = false
        self.view.addSubview(userInfoView)
        userInfoView.clickVersionClosure = {
            LYHUDUtils.showToast(LYlocalizedString("Version") + ":1.0.0")
        }
        
        userInfoView.clickSetupButtonClosure = { [weak self] in

            guard let self = self else { return }
            let settingVC = LYSettingViewController()
            self.navigationController?.pushViewController(settingVC, animated: true)
        }
        
        userInfoView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(-20)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(80)
        }
        
        scrollView.isDirectionalLockEnabled = true
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
//        scrollView.backgroundColor = .green
        scrollView.showsVerticalScrollIndicator = false
        scrollView.isUserInteractionEnabled = true
        view.addSubview(scrollView)
//        contentView.backgroundColor = .red
        scrollView.addSubview(contentView)
        self.view.addSubview(scrollView)
        
        scrollView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(userInfoView.snp.bottom)
            make.bottom.equalToSuperview().offset(-customTabBarHeight - bottomMargin)
        }
        
        contentView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.width.equalTo(scrollView)
        }

        let model1 = LYActionModel(imageName: "user_tips_1",
                                   title: LYlocalizedString("All"))
        let model2 = LYActionModel(imageName: "user_tips_2",
                                   title: LYlocalizedString("Applying"))
        let model3 = LYActionModel(imageName: "user_tips_3",
                                   title: LYlocalizedString("Repayment"))
        let model4 = LYActionModel(imageName: "user_tips_4",
                                   title: LYlocalizedString("Finish"))
        actionView.delegate = self
        actionView.setActionView(models: [model1, model2, model3, model4])
        
        contentView.addSubview(actionView)
        let aspectRatio = 117.0 / 336.0
        let leftMargin = 10.0
        let width = SCREEN_WIDTH - leftMargin * 2
        let height = width * aspectRatio
        actionView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(15)
            make.top.equalToSuperview()
            make.height.equalTo(height)
        }
        
        let aspectRatio1 = 155.0 / 326.0
        let leftMargin1 = 15.0
        let width1 = SCREEN_WIDTH - leftMargin1 * 2
        let height1 = width1 * aspectRatio1
        contentView.addSubview(privacyAggrementImageView)
        contentView.addSubview(serviceImageView)
        contentView.addSubview(privacyAggrementButton)
        contentView.addSubview(serviceButton)
        serviceImageView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(leftMargin1)
            make.top.equalTo(actionView.snp.bottom).offset(15)
            make.height.equalTo(height1)
        }
        
        serviceButton.snp.makeConstraints { make in
            make.edges.equalTo(serviceImageView)
        }
        
        privacyAggrementImageView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(leftMargin1)
            make.top.equalTo(serviceImageView.snp.bottom).offset(15)
            make.height.equalTo(height1)
            make.bottom.equalTo(contentView)
        }
        
        privacyAggrementButton.snp.makeConstraints { make in
            make.edges.equalTo(privacyAggrementImageView)
        }
        
        self.view.layoutIfNeeded()
        scrollView.contentSize = contentView.frame.size
    }
    
    // 刷新列表
    func updateData() {
        userInfoView.updateData()
    }
    
    @objc func serviceButtonAction(_ sender: UIButton) {
        let url = APIDomain.h5.rawValue + "/brotheraire"
        self.pushToWebViewController(url)
    }
    
    @objc func privacyButtonAction(_ sender: UIButton) {
        let url = APIDomain.h5.rawValue + "/hangry"
        self.pushToWebViewController(url)
    }
}

extension LYUserViewController: LYUserActionViewDelegate {
    
    func didSelected(actionView: LYUserActionView, model: LYActionModel?, didClick index: Int) {
        print(index)
        if let mainVC = UIApplication.shared.currentKeyWindow?.rootViewController as? LYMainViewController {
            mainVC.changeViewController(to: 1)
            if let vc = mainVC.children.first,
               let orderVC = (vc as? LYNavigationController)?.viewControllers.first as? LYOrderViewController {
                orderVC.updateData(tab: "\(index + 1)")
            }
        }
    }
    
    func pushToWebViewController(_ url: String) {
        let webviewVC = LYWebViewController()
        webviewVC.urlString = url
        UIApplication.topViewController()?.navigationController?.pushViewController(webviewVC, animated: true)
    }
}
